package com.zmn.oms.services.impl.entrust;

import com.zmn.oms.model.entity.entrust.EntrustOrderQuery;
import com.zmn.oms.model.entity.entrust.EntrustOrder;
import com.zmn.oms.persistence.interfaces.entrust.EntrustOrderDao;
import com.zmn.oms.services.interfaces.entrust.EntrustOrderService;
import com.zmn.oms.services.interfaces.orderencrypt.EntrustOrderEncryptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 类描述：委托单实现类
 *
 * @author lhl
 * @date 2020/01/02 16:37
 */
@Service("entrustOrderService")
public class EntrustOrderServiceImpl implements EntrustOrderService {

    @Resource
    private EntrustOrderDao entrustOrderDao;
    @Autowired
    private EntrustOrderEncryptService entrustOrderEncryptService;

    @Override
    public EntrustOrder findByKey(Long entrustOrderId) {
        if (entrustOrderId == null) {
            return null;
        }
        EntrustOrder entrustOrder = entrustOrderDao.findByKey(entrustOrderId);
        return entrustOrderEncryptService.decryptEntrustOrder(entrustOrder);
    }

    @Override
    public int updateByKey(EntrustOrder entrustOrder) {
        if (entrustOrder == null) {
            return 0;
        }
        entrustOrder.setUpdateTime(new Date());
        EntrustOrder entrustOrderCopy = entrustOrderEncryptService.encryptEntrustOrder(entrustOrder);
        return entrustOrderDao.updateByKey(entrustOrderCopy);
    }

    @Override
    public int insertEntrustOrder(EntrustOrder entrustOrder) {
        if (entrustOrder == null) {
            return 0;
        }
        EntrustOrder entrustOrderCopy = entrustOrderEncryptService.encryptEntrustOrder(entrustOrder);
        int count = entrustOrderDao.insertEntrustOrder(entrustOrderCopy);
        entrustOrder.setEntrustId(entrustOrderCopy.getEntrustId());
        return count;
    }

    @Override
    public List<EntrustOrder> listOrders(EntrustOrderQuery query) {
        int count = entrustOrderDao.listOrdersCount(query);
        List<EntrustOrder> listOrders = entrustOrderDao.listOrders(query);
        List<EntrustOrder> entrustOrdersCopy = entrustOrderEncryptService.batchDecryptEntrustOrder(listOrders);
        query.setTotalCount(count);
        return entrustOrdersCopy;
    }
}
